package com.itic.appbase.applications.sys.relation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.applications.sys.relation.persistence.dao.UserRoleDao;
import com.itic.appbase.applications.sys.relation.persistence.model.UserRole;
import com.itic.appbase.applications.sys.relation.persistence.model.UserRoleExample;
import com.itic.appbase.applications.sys.role.persistence.model.Role;

@Service
@Transactional
public class UserRoleService
        extends CrudService<UserRoleDao, UserRole, UserRoleExample> {
    @Autowired
    private UserRoleDao userRoleDao;

    /**
     * 根据用户id获取用户拥有的角色List
     * 
     * @param userId
     * @return List<Role>
     */
    public List<Role> getUserOwnRolers(String userId) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("userId", userId);
        paramMap.put("dbTrue", DBConst.TRUE);
        return userRoleDao.getRolersByUserId(paramMap);
    }

    /**
     * 根据用户id获取用户未分配的角色列表
     * 
     * @param userId
     * @return
     */
    public List<Role> getUserUnOwnRolers(String userId) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("userId", userId);
        paramMap.put("dbTrue", DBConst.TRUE);
        return userRoleDao.getUnOwnRolersByUserId(paramMap);
    }

    /**
     * 根据userId删除用户角色关系
     * 
     * @param userId 用户id
     * @return
     */
    public String deleteRoleByUserId(String userId) {
        int code = userRoleDao.deleteRoleByUserId(userId);
        if (code >= 0) {
            return SUCCESS;
        } else {
            return FAILED;
        }
    }

}